﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MapAndCharacter_Filip
{
    public class Sprite
    {
        private ContentManager p_contentManager;
        protected SpriteBatch p_spriteBatch;
        protected Texture2D p_image;
        private Rectangle rect;
        private Vector2 p_position; // upper left corner
        private Color p_color;

        public Sprite(ContentManager c, SpriteBatch s)
        {
            this.p_contentManager = c;
            this.p_spriteBatch = s;
            this.p_image = null;
            this.p_position = Vector2.Zero;
            this.p_color = Color.White;
        }

        public Texture2D Image
        {
            get { return this.p_image; }
            set { this.p_image = value; }
        }

        public Vector2 Position
        {
            get { return this.p_position; }
            set { this.p_position = value; }
        }

        public void SetImage(Texture2D image)
        {
            p_image = image;
            rect = new Rectangle(rect.X, rect.Y, image.Width, image.Height);
        }

        public Color Color
        {
            get { return this.p_color; }
            set { this.p_color = value; }
        }
        
        public bool Load(string assetName, int width=0, int height=0)
        {
            try
            {
                this.p_image = p_contentManager.Load<Texture2D>(assetName);
                if (width == 0 || height == 0)
                {
                    width = p_image.Width;
                    height = p_image.Height;
                }
                rect = new Rectangle(0, 0, width, height);
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        public virtual void Draw()
        {
            p_spriteBatch.Draw(p_image, rect, this.Color);
            //p_spriteBatch.Draw(this.Image, this.Position, this.Color);
        }
    }
}